package com.kookapi.asserts.impl;


import com.kookapi.asserts.AssertionComponent;
import com.kookapi.asserts.result.AssertionResult;
import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author bairimengchang
 * @version 1.0
 * @project java-test
 * @description 响应时间断言组件
 * @date 2025/9/29
 */
@Component("responseTimeAssertionComponent")
@Slf4j
public class ResponseTimeAssertionComponent implements AssertionComponent {
    @Override
    public AssertionResult execute(Response response, Map<String, Object> context) {
        log.info("执行响应时间断言");
        try {
            // 假设最大响应时间为1秒
            long maxTime = 1000L;

            // 获取响应的实际响应时间
            long actualTime = response.getTime();

            if (actualTime <= maxTime) {
                return AssertionResult.success(String.format("响应时间验证通过: %dms <= %dms", actualTime, maxTime));
            } else {
                return AssertionResult.failure(String.format("响应时间超时: %dms > %dms", actualTime, maxTime));
            }
        } catch (Exception e) {
            return AssertionResult.failure("响应时间断言配置解析错误: " + e.getMessage());
        }
    }
}
